package com.heima.bos.web.action.impl;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.task.Task;
import org.activiti.engine.task.TaskQuery;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.heima.bos.domain.User;
import com.heima.bos.domain.Workordermanage;
import com.heima.bos.service.WorkordermanageService;
import com.heima.bos.utils.BOSUtils;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
@Controller
@Scope("prototype")
public class TaskAction extends ActionSupport{
	@Resource
	private ProcessEngine processEngine;
	private String taskId;//属性注入任务id
	@Resource
	private WorkordermanageService workordermanageService;
	/**
	 * 仓库管理员出库
	 */
	public String outStore(){
		processEngine.getTaskService().complete(taskId);
		return findPersonalTask();
	}
	/**
	 * 取派员配送
	 */
	public String transferGoods(){
		processEngine.getTaskService().complete(taskId);
		return findPersonalTask();
	}
	/**
	 * 客户签收
	 */
	public String receive(){
		processEngine.getTaskService().complete(taskId);
		return findPersonalTask();
	}
	/**
	 * 办理个人任务
	 * 	是否审核
	 */
	//审核是否通过
	private String check;
	//办理个人任务
	public String checkWorkOrderManage(){
		Workordermanage workordermanage = null;
		Map<String, Object> map = null;
		map = processEngine.getTaskService().getVariables(taskId);
		if(check == null){
			ActionContext.getContext().getValueStack().set("map", map);
			return "toCheckUI";
		}else{
			workordermanage = (Workordermanage) map.get("业务数据");
			workordermanage.setManagerCheck("1");
			workordermanageService.update(workordermanage);
			processEngine.getTaskService().setVariable(taskId, "业务数据", workordermanage);
			Map<String, Object> varibales = new HashMap<String, Object>();
			varibales.put("check", check);
			processEngine.getTaskService().complete(taskId, varibales);
			if(check.equals("0")){
				workordermanage.setStart("0");
				workordermanageService.update(workordermanage);
			}
		}
		return "toPersonalList";
	}
	
	/**
	 * 查询个人任务
	 */
	public String findPersonalTask(){
		User user = (User) BOSUtils.getSession().getAttribute("user");
		String userId = user.getId();
		TaskQuery query = processEngine.getTaskService().createTaskQuery();
		query.taskAssignee(userId);
		List<Task> list = query.list();
		ActionContext.getContext().getValueStack().set("list", list);
		return "toPersonTaskList";
	}
	/**
	 * 拾取任务
	 */
	public String takeTask(){
		User user = (User) BOSUtils.getSession().getAttribute("user");
		String userId = user.getId();
		processEngine.getTaskService().claim(taskId, userId);
		return "toGroupList";
	}
	/**
	 * 查询组任务
	 */
	public String findGroupTask(){
		//获取当前用户id
		User user = (User) BOSUtils.getSession().getAttribute("user");
		String userId = user.getId();
		//获取组任务查询对象
		TaskQuery query = processEngine.getTaskService().createTaskQuery();
		query.taskCandidateUser(userId);
		List<Task> list = query.list();
		ActionContext.getContext().getValueStack().set("list", list);
		return "toGroupTaskList";
	}
	/**
	 * 获取流程变量
	 */
	public void showData() throws IOException{
		Map<String, Object> map = processEngine.getTaskService().getVariables(taskId);
		ServletActionContext.getResponse().setContentType("text/html;charset=UTF-8");
		ServletActionContext.getResponse().getWriter().print(map);
		System.out.println(map);
	}
	public void setTaskId(String taskId) {
		this.taskId = taskId;
	}
	public void setCheck(String check) {
		this.check = check;
	}
	public String getTaskId() {
		return taskId;
	}
	public String getCheck() {
		return check;
	}
}
